package com.example.stray_animal_management_system.admin.controller;

import com.example.stray_animal_management_system.admin.service.UserLoginInfoService;
import com.example.stray_animal_management_system.entity.UserLoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: panpan
 * @time: 2025/11/9
 */
@RestController
@RequestMapping("/api/admin/user-login-info")
public class UserLoginInfoController {

    @Autowired
    private UserLoginInfoService userLoginInfoService;

    /**
     * 获取所有登录信息
     */
    @GetMapping("/list")
    public Map<String, Object> list() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<UserLoginInfo> loginInfoList = userLoginInfoService.getAllLoginInfo();
            result.put("code", 200);
            result.put("msg", "获取成功");
            result.put("data", loginInfoList);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取失败：" + e.getMessage());
            result.put("data", null);
        }
        return result;
    }

    /**
     * 记录用户登录信息
     */
    @PostMapping("/save")
    public Map<String, Object> save(@RequestBody UserLoginInfo loginInfo) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (loginInfo.getUserId() == null) {
                result.put("code", 400);
                result.put("msg", "用户ID不能为空");
                return result;
            }
            boolean success = userLoginInfoService.saveLoginInfo(loginInfo);
            if (success) {
                result.put("code", 200);
                result.put("msg", "保存成功");
            } else {
                result.put("code", 500);
                result.put("msg", "保存失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "保存失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 删除登录信息
     */
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = userLoginInfoService.deleteLoginInfo(id);
            if (success) {
                result.put("code", 200);
                result.put("msg", "删除成功");
            } else {
                result.put("code", 500);
                result.put("msg", "删除失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "删除失败：" + e.getMessage());
        }
        return result;
    }
}

